package org.dreamwork.jasmine2.web.html;

import org.dreamwork.jasmine2.parser.Tag;
import org.dreamwork.jasmine2.web.controls.WebContainer;
import org.dreamwork.jasmine2.web.controls.UserControl;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.util.StringConverter;

import java.io.PrintWriter;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-1-30
 * Time: 22:51:45
 */
public class GenericHtmlControl extends WebContainer {
    public GenericHtmlControl () {
        viewStateEnabled = false;
    }

    public GenericHtmlControl (String tagName) {
        htmlTagName = tagName;
        viewStateEnabled = false;
    }

    public GenericHtmlControl (Tag tag) {
        htmlTagName = tag.getName ();
        String[] names = tag.getAttributeNames ();
        for (String name : names) {
            setAttribute (name, tag.getAttribute (name));
        }
    }

    public IWebControl copy () {
        GenericHtmlControl copy = new GenericHtmlControl (htmlTagName);
        for (Object o : attrs.keySet ()) {
            String key = (String) o;
            copy.attrs.setProperty (key, attrs.getProperty (key));
        }

        for (Object o : dynamicAttributes.keySet ()) {
            String key = (String) o;
            copy.dynamicAttributes.put (key, dynamicAttributes.get (key));
        }
        copy.page = page;
        if (name != null)
            copy.setAttribute ("name", name);
        return copy;
    }

    protected void beginTag (PrintWriter writer) throws IOException {
        if ("br".equalsIgnoreCase (htmlTagName))
            writer.write ("<br>");
        else if ("hr".equalsIgnoreCase (htmlTagName)) {
            writer.write ("<hr");
            renderAttributes (writer);
            writer.write ("/>");
        } else if ("input".equalsIgnoreCase (htmlTagName)) {
            writer.write ("<input");
            renderAttributes (writer);
            writer.write ("/>");
        } else
            super.beginTag (writer);
    }

/*
    @Override
    public String getClientId () {
        if (!StringConverter.isEmpty (clientId)) {
            return clientId;
        } else {
            if (!StringConverter.isEmpty (id) && !id.startsWith ("_auto_")) {
                if (page instanceof UserControl) {
                    if (!StringConverter.isEmpty (page.getId ())) return page.getId () + "_" + id;
                    return id;
                }
                return id;
            } else {
                String expression = getDynamicAttribute ("clientid");
                if (!StringConverter.isEmpty (expression)) {
                    String cid = null;
                    try {
                        cid = StringConverter.contvertToString (page.eval(expression));
                    } catch (Exception e) {
                        e.printStackTrace ();
                    }
                    if (!StringConverter.isEmpty (cid)) {
                        if (page instanceof UserControl)
                            return page.getId () + "_" + cid;
                        return cid;
                    }
                }
            }
        }
        return super.getClientId ();
    }
*/

    protected void endTag (PrintWriter writer) {
        if (!"br".equalsIgnoreCase (htmlTagName) && !"hr".equalsIgnoreCase (htmlTagName) && !"input".equalsIgnoreCase (htmlTagName))
            super.endTag (writer);


    }
}